﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace CuaHangHoa
{
    public partial class form_SuaMatHang : Form
    {
        public form_SuaMatHang()
        {
            InitializeComponent();
        }


        public delegate void DoEvent();
        public event DoEvent RefreshDgv;


        public void Loaddata_gv(quanlymathang_DTO mh)
        {
            txt_mahang.Text = mh.mamathang.ToString();
            txt_tenhang.Text = mh.tenhang;
            if (mh.xuatxu == 1)
            {
                cbx_xuatxu.Text = "Đà Lạt";
            }
            
            if (mh.xuatxu == 2)
            {
                cbx_xuatxu.Text = "Đài Loan";
            }

            //cbx_xuatxu.Text = mh.xuatxu ;
            txt_donvitinh.Text = mh.donvitinh;

            txt_soluongton.Text = mh.soluongton.ToString();
            txt_gianhap.Text = mh.gianhap.ToString();
            txt_tinhtrang.Text = mh.tinhtrang;
            txt_ghichu.Text = mh.ghichu;
            txt_dongia.Text = mh.dongia.ToString();
            if (mh.idlloai == 1)
            {
                cbx_idlloai.Text = "Hoa";
            }
            if (mh.xuatxu == 1)
            {
                cbx_idlloai.Text = "Chậu";
            }

        }


        private void CapNhatMH()
        {
            quanlymathang_DTO mh = new quanlymathang_DTO();

            mh.mamathang = int.Parse(txt_mahang.Text);
            mh.tenhang = txt_tenhang.Text;
            if (cbx_xuatxu.Text == "Đà Lạt")
            {
                mh.xuatxu = 1;
            }
            if (cbx_xuatxu.Text == "Đài Loan")
            {
                mh.xuatxu = 2;
            }

         
            mh.donvitinh = txt_donvitinh.Text;
            mh.soluongton = int.Parse (txt_soluongton.Text);
            mh.gianhap = int.Parse (txt_gianhap.Text);
            mh.tinhtrang = txt_tinhtrang.Text;
            mh.ghichu = txt_ghichu.Text;
            mh.dongia  = int.Parse (txt_dongia.Text);


            if (cbx_idlloai.Text == "Hoa")
            {
                mh.idlloai = 1;
            }
            if (cbx_xuatxu.Text == "Chậu")
            {
                mh.idlloai = 2;
            }

            quanlymathang_BUS bus = new quanlymathang_BUS();
            int kq = bus.CapNhatMatHang(mh);

            if (kq == 1)
            {
                this.RefreshDgv();
                //   this.RefreshDgv();
                MessageBox.Show("Sửa thành công");
            }
            else
            {
                MessageBox.Show("Sửa thất bại");
            }
            txt_tenhang.Focus();
        }


        private void form_SuaMatHang_Load(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
          
            txt_tenhang.Text = "";
            cbx_xuatxu .Text = "";
            txt_donvitinh .Text = "";
            txt_soluongton.Text = "";
            txt_gianhap.Text = "";
            txt_tinhtrang.Text = "";
            txt_ghichu.Text = "";
            txt_dongia.Text = "";
            cbx_idlloai.Text  = "";
            txt_tenhang.Focus();
        }

        private void bt_Sửa_Click(object sender, EventArgs e)
        {
            CapNhatMH();
            errorProvider1.Clear();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txt_donvitinh_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }

        private void txt_soluongton_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                errorProvider1.SetError(txt_soluongton , "Chi được nhập số");

            }
        }

        private void txt_gianhap_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                errorProvider1.SetError(txt_gianhap , "Chi được nhập số");

            }
        }

        private void txt_dongia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                errorProvider1.SetError(txt_dongia, "Chi được nhập số");

            }
        }

        private void txt_donvitinh_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
